﻿using M_Ingredient.API.Application.Command.IngredientCommand;
using M_Ingredient.Domain;
using M_Ingredient.Repository.Interface;
using MediatR;

namespace M_Ingredient.API.Application.CommandHandler.IngredientCommandHandler
{
    public class IngredientBackDanYuanCommandHandler : IRequestHandler<IngredientBackDanYuanCommand, List<Ingredient>>
    {
        protected readonly IIngredientRepository ingredientRepository;

        public IngredientBackDanYuanCommandHandler(IIngredientRepository ingredientRepository)
        {
            this.ingredientRepository = ingredientRepository;
        }

        public async Task<List<Ingredient>> Handle(IngredientBackDanYuanCommand request, CancellationToken cancellationToken)
        {   // 使用 LINQ 的 Contains 方法来筛选
            var res = await ingredientRepository.GetAllAsync();

            var ids = request.IngredientId;
            var matchedInspections = res.Where(inspection => ids.Any(idItem => idItem == inspection.IngredientId)).ToList();
            // return matchedInspections;
            return matchedInspections;

        }
    }
}
